package io.rong.imkit.widget.adapter;

import android.view.ViewGroup;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/** Created by zhy on 16/6/28. */
public class WrapperUtils {
    public interface SpanSizeCallback {
        int getSpanSize(
                GridLayoutManager layoutManager,
                GridLayoutManager.SpanSizeLookup oldLookup,
                int position);
    }

    public static void onAttachedToRecyclerView(
            RecyclerView.Adapter innerAdapter,
            RecyclerView recyclerView,
            final SpanSizeCallback callback) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup =
                    gridLayoutManager.getSpanSizeLookup();

            gridLayoutManager.setSpanSizeLookup(
                    new GridLayoutManager.SpanSizeLookup() {
                        @Override
                        public int getSpanSize(int position) {
                            return callback.getSpanSize(
                                    gridLayoutManager, spanSizeLookup, position);
                        }
                    });
            gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
        }
    }

    public static void setFullSpan(RecyclerView.ViewHolder holder) {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {

            StaggeredGridLayoutManager.LayoutParams p =
                    (StaggeredGridLayoutManager.LayoutParams) lp;

            p.setFullSpan(true);
        }
    }
}
